<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\property\auth;

use think\facade\Db;
use app\common\repositories\BaseRepository;
use app\common\dao\property\auth\PropertyAuthRoleDao;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class PropertyAuthRoleRepository
 *
 * @mixin PropertyAuthRoleDao
 */
class PropertyAuthRoleRepository extends BaseRepository
{

    public function __construct(PropertyAuthRoleDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 查询数据列表
     * @param array $where
     * @param $page
     * @param $limit
     * @param int|null $propertyId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-17 22:01
     */
    public function getPropertyList(array $where, $page, $limit, int $userId = null)
    {
        $query = $this->dao->search($where, $userId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'propertyInfo' => function ($query) {
                    $query->field('id,name');
                    $query->bind(['property_name' => 'name']);
                }
            ])
            ->order('id', 'desc')
            ->select();
        return compact('list', 'count');
    }

    /**
     * 添加数据
     * @param array $data
     * @param int $propertyId
     * @param int $userId
     * @return mixed
     *
     * @author Amos
     * 2024-01-17 22:01
     */
    public function addInfo(array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $userId) {
            $info = $this->dao->create($data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($data['property_id'], $userId, 'AUTH.ROLE.ADD', $data);
            return $info;
        });
    }

    /**
     * 编辑数据
     * @param $info
     * @param array $data
     * @param int $userId
     * @return mixed
     *
     * @author Amos
     * 2024-01-17 22:01
     */
    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            $res = $this->dao->update($info['id'], $data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'AUTH.ROLE.EDIT', $data);
            return $res;
        });
    }

    /**
     * 批量删除数据
     * @param array $ids
     * @param int $userId
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-17 22:01
     */
    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    /**
     * 删除单条数据
     * @param $info
     * @param int $userId
     * @return void
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-01-17 22:01
     */
    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'AUTH.ROLE.DEL', $info);
        $this->dao->delete($info['id']);
    }

}